﻿using JNPF.Basics.Models;
using JNPF.Data;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 用户关系
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.11.07
    /// </summary>
    internal class UserRelationService : DataFactory, UserRelationIService
    {
        /// <summary>
        /// 根据用户主键获取列表
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns></returns>
        public async Task<List<UserRelationEntity>> GetListByUserId(string userId)
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<UserRelationEntity>().Where(m => m.UserId == userId).OrderBy(o => o.CreatorTime).ToListAsync();
            }
        }
        /// <summary>
        /// 根据对象主键获取列表
        /// </summary>
        /// <param name="objectId">对象主键</param>
        /// <returns></returns>
        public async Task<List<UserRelationEntity>> GetListByObjectId(string objectId)
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<UserRelationEntity>().Where(m => m.ObjectId == objectId).OrderBy(o => o.CreatorTime).ToListAsync();
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<UserRelationEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<UserRelationEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids">主键值</param>
        public async Task Delete(string[] ids)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                foreach (var item in ids)
                {
                    await db.DeleteAsync<UserRelationEntity>(m => m.Id == item);
                }
                await db.CommitTransactionAsync();
            }
        }

        /// <summary>
        /// 根据objecttype和id删除
        /// </summary>
        /// <param name="ids">主键值</param>
        public async Task DeleteByObject(UserRelationEntity entity)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                await db.DeleteAsync<UserRelationEntity>(m => m.ObjectType == entity.ObjectType && m.ObjectId == entity.ObjectId);
                await db.CommitTransactionAsync();
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entitys">实体对象</param>
        public async Task Create(List<UserRelationEntity> entitys)
        {
            using (var db = this.Basic)
            {
                await db.InsertAsync(entitys);
            }
        }
    }
}
